<template>
    <div class="login-page">
        <div class="login-box">
            <div class="avatar-box">
                <img src="@/assets/img/avatar.png" alt="avatar">
            </div>
            <!-- form 表单 -->
            <el-form :model="loginForm" status-icon :rules="loginRules" ref="loginFormRef" label-width="0px" class="Form">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-yonghuming" />
                </el-form-item>
                <el-form-item  prop="password">
                    <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-mima" />
                </el-form-item>
                <el-form-item class="login-btn">
                    <el-button type="primary" @click="submitForm">登录</el-button>
                    <el-button type="info" @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { setToken } from '@/utils/auth'
import { login } from '@/api/user'
export default {
  name: 'LoginPage',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 登录
    submitForm () {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (valid) {
          try {
            const res = await login(this.loginForm)
            setToken(res.data.token)
            // 保存提示标志
            sessionStorage.setItem('isShowTip', true)
            // 跳转到首页
            this.$router.push('/index')
            this.$message({
              type: 'success',
              message: '登录成功'
            })
          } catch (error) {
            console.log(error)
          }
        }
      })
    },
    // 重置
    resetForm () {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
    height: 100%;
    background-color: #2b4b6b;
    .login-box {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 400px;
        height: 300px;
        background-color: #fff;
        border-radius: 5px;
    }
    .avatar-box {
        width: 100px;
        height: 100px;
        margin: 0 auto;
        padding: 5px;
        transform: translateY(-50%);
        background-color: #fff;
        border: 1px solid #ccc;
        border-radius: 50%;
        box-shadow: 0 0 10px #ddd;
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
        }
    }
    .Form {
        position: absolute;
        bottom: 30px;
        width: 100%;
        padding: 0 40px;
        box-sizing: border-box;
        .login-btn {
            display: flex;
            justify-content: flex-end;
        }
    }
}
</style>
